package com.java110.mall.acct.cmd.integral;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.CmdContextUtils;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.integral.IntegralDto;
import com.java110.dto.integral.PreStoreUserIntegralDto;
import com.java110.dto.integral.IntegralDetailDto;
import com.java110.dto.user.UserDto;
import com.java110.intf.acct.IIntegralV1InnerServiceSMO;
import com.java110.intf.user.IUserV1InnerServiceSMO;
import com.java110.po.integral.IntegralDetailPo;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * admin用户修改积分金额
 */
@Java110Cmd(serviceCode = "integral.changeIntegralAmount")
public class ChangeIntegralAmountCmd extends Cmd {

    @Autowired
    private IIntegralV1InnerServiceSMO integralV1InnerServiceSMOImpl;

    @Autowired
    private IUserV1InnerServiceSMO userV1InnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        /**
         * detailType: '',
         *                 integralName: '',
         *                 integralId: '',
         *                 amount: '',
         *                 value:'',
         *                 link: '',
         */

        Assert.hasKeyAndValue(reqJson,"integralId","积分编号不能为空");
        Assert.hasKeyAndValue(reqJson,"detailType","类型不能为空");
        Assert.hasKeyAndValue(reqJson,"value","数量不能为空");

        if(IntegralDetailDto.DETAIL_TYPE_IN.equals(reqJson.getString("detailType"))){
            return;
        }

        IntegralDto integralDto = new IntegralDto();
        integralDto.setIntegralId(reqJson.getString("integralId"));
        List<IntegralDto> integralDtos = integralV1InnerServiceSMOImpl.queryIntegrals(integralDto);

        Assert.listOnlyOne(integralDtos,"积分账户不存在");

        double amount = Double.parseDouble(integralDtos.get(0).getAmount());
        double value  = reqJson.getDoubleValue("value");
        if(value > amount){
            throw new CmdException("积分不足");
        }
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {

        String userId = CmdContextUtils.getUserId(context);

        UserDto userDto = new UserDto();
        userDto.setUserId(userId);
        List<UserDto> userDtos = userV1InnerServiceSMOImpl.queryUsers(userDto);
        Assert.listOnlyOne(userDtos,"用户不存在");
        // todo 预存
        if(IntegralDetailDto.DETAIL_TYPE_IN.equals(reqJson.getString("detailType"))){
            PreStoreUserIntegralDto preStoreUserIntegralDto = new PreStoreUserIntegralDto();
            preStoreUserIntegralDto.setIntegralId(reqJson.getString("integralId"));
            preStoreUserIntegralDto.setAmount(reqJson.getDoubleValue("value"));
            preStoreUserIntegralDto.setOrderId("-1");
            preStoreUserIntegralDto.setRemark(userDtos.get(0).getName()+"手工赠送积分，"+reqJson.getString("remark"));
            integralV1InnerServiceSMOImpl.preStoreUserIntegral(preStoreUserIntegralDto);
            return;
        }

        // todo 扣款

        IntegralDetailPo integralDetailPo = new IntegralDetailPo();
        integralDetailPo.setIntegralId(reqJson.getString("integralId"));
        integralDetailPo.setAmount(reqJson.getDoubleValue("value") + "");
        integralDetailPo.setRemark(userDtos.get(0).getName()+"手工扣除积分,"+reqJson.getString("remark"));
        integralV1InnerServiceSMOImpl.withholdIntegral(integralDetailPo);
    }
}
